/****************************************************
          File Name:link_unlink.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Mon Dec  8 19:24:53 2014
*****************************************************/
// unlink remove link when fd has been closed
#include "dir.h"

int main(int argc, char *argv[])
{
    int ln;
    int fd;
    char cmd[256];

    if(argc < 3)
    {
        fprintf(stderr, "%s oldpath newpath\n", argv[0]);
        exit(1);
    }
    
    fd = open(argv[1], O_RDONLY, 0);
    if(fd == -1)
        err("open");

    printf("now link %s to %s\n", argv[1], argv[2]);

    ln = link(argv[1], argv[2]);
    if(ln != 0)
        err("link");

    printf("now show the result:\n");
    snprintf(cmd, 256, "ls -li %s", argv[1]);

    system(cmd);
    sleep(3);

    printf("now, remove the link(NOT CLOSE fd yet), and show the result\n");
    ln = unlink(argv[2]);
    if(ln !=0)
        err("unlink");

    system(cmd);
    sleep(3);

    printf("now, show result (fd has been closed):\n");
    close(fd);
    system(cmd);

    return 0;
}
